<template>
	<view class="article">
		<view class="arcticle-vedio" v-if="arctileObj">
			<view>
				<video id="myVideo"
					:src="videoUrl"
					@error="videoErrorCallback" object-fit="fill" controls autoplay
					:poster="imgUrl + arctileObj.image"></video>
			</view>
			<view class="title">{{arctileObj.title}}</view>
			<view class="uni-flex list-info">
				<view class="author">
					<image v-if="arctileObj.expertImage" :src="imgUrl + arctileObj.expertImage"></image>
					<image v-else src="https://staticfile-yj.obs.cn-north-4.myhuaweicloud.com/avatar.png"></image>
					<text>{{arctileObj.expertName}}</text>
				</view>
				<view class="see">
					<uni-icons type="eye" color="#9498A0" size="14"></uni-icons>
					<text>{{arctileObj.browseNum}}</text>
					<uni-fav style="float: right; margin-left: 30rpx;" :checked="isFav" :circle="true" bg-color="#B4B5B7" @click="setFav(isFav)"
						bg-color-checked="rgba(255, 90, 90, 0.08)" fg-color="#ffffff" fg-color-checked="#FF5A5A" />
				</view>
			</view>
		</view>
		<view v-else>
			<uni-load-more :status="LoadMore" :content-text="contentText"></uni-load-more>
		</view>
	</view>
</template>

<script>
	import {
		baseurl,
		detailKnowledgeCourse,
		deleteCollection,
		addCollection
	} from "@/api/user.js"
	let imgUrl = 'https://nongmao.obs.cn-north-4.myhuaweicloud.com/'
	export default {
		data() {
			return {
				imgUrl,
				isFav: false,
				parmas: '',
				arctileObj: null,
				showLoadMore: false,
				videoUrl:'',
				LoadMore: 'nomore',
				contentText: {
					contentdown: '...',
					contentrefresh: '加载中',
					contentnomore: '暂无数据'
				},
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log(option.id); //打印出上个页面传递的参数。
			this.parmas = option.id;
			this.showLoadMore = true;
			console.log("onload", this.arctileObj)
			this.detailKnowledgeCourse(this.parmas);
		},
		onPullDownRefresh(){
			this.showLoadMore = true;
			this.detailKnowledgeCourse(this.parmas);
		},
		methods: {
			async detailKnowledgeCourse(options) {
				const res = await detailKnowledgeCourse(options);
				console.log(res)
				this.arctileObj = res.returnObj;
				if(this.arctileObj.myFavorite > 0){
					this.isFav = true
				}else{
					this.isFav = false
				}
				if (this.arctileObj.type == '2') {
					uni.downloadFile({
						url: `${baseurl}/nb-as/1.0.1/FileService/downloadFile?fileId=${this.arctileObj.video}`, 
						success: (res) => {
							if (res.statusCode === 200) {
								console.log("image", res);
								this.videoUrl = res.tempFilePath
							}
						}
					});
				}else{
					this.imgUrl = "https://staticfile-yj.obs.cn-north-4.myhuaweicloud.com/none.png"
				}
			},
			async addCollection(options){
				const res = await addCollection(options);
				if(res.success){
					uni.showToast({
						title:'收藏成功！'
					})
					this.isFav = true;
				}
			},
			
			async deleteCollection(options){
				const res = await deleteCollection(options);
				if(res.success){
					uni.showToast({
						title:'已取消收藏！'
					})
					this.isFav = false
				}
			},
			setFav() {
				let options = {
					serviceType:'2',
					serviceId:this.arctileObj.id
				}
				if(this.isFav){
					this.deleteCollection([options])
				}else{
					this.addCollection(options)
				}
				this.$forceUpdate()
			},
			videoErrorCallback: function(e) {
				uni.showModal({
					content: e.target.errMsg,
					showCancel: false
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-image: url("https://staticfile-yj.obs.myhuaweicloud.com/course-bg.png");
		background-repeat: no-repeat;
		background-size: 100% auto;
		background-color: #fff;
	}

	.article {
		// padding: 30rpx;
	}

	video {
		width: 690rpx;
		width: 100%;
		max-height: 400px;
	}

	.title {
		color: #283241;
		font-size: 40rpx;
		font-weight: 500;
		line-height: 28px;
		padding: 15px 30rpx;
	}
	
	.list-info .author{
		width: calc(100% - 120px);
	}
	
	.summary {
		background-color: #F5F8FB;
		border-radius: 8px;
		padding: 24rpx 32rpx;
		margin: 30px 0;
	}

	.summary .title {
		font-size: 36rpx;
		line-height: 26px;
		color: #283241;
		flex-flow: row nowrap;
		justify-content: flex-start;
		align-items: center;
	}

	.summary .con {
		line-height: 26px;
		font-size: 28rpx;
		font-weight: 400;
		color: #283241;
	}

	.art text {
		font-size: 28rpx;
		line-height: 26px;
	}

	.art image {
		max-width: 90%;
		margin: 15px auto;
		display: block;
	}
</style>